﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLBV.Models;
using System.Web.Helpers;
using System.IO;

namespace QLBV.Controllers
{ 
    public class NewsController : Controller
    {
        private QLBVContext db = new QLBVContext();

        //
        // GET: /News/

        public ViewResult ListNews()
        {
            IList<News> Tintuc;
            Tintuc = (from tt in db.News
                      orderby tt.ID descending
                      select tt).ToList();

            return View(Tintuc);  
        }

        public ActionResult ContentNews(int ID)
        {
            //var Chitiet = (from tt in db.News
            //               where tt.ID == ID
            //               select tt).FirstOrDefault();

            //return View(Chitiet);

            var nd = db.News.Where(n => n.ID == ID).FirstOrDefault();
            ViewBag.Noidung = new MvcHtmlString(nd.Content.ToString());
            return View(nd);


        }

        //
        // GET: /News/Details/5

        public ViewResult DSTin()
        {
            IList<News> DStin;
            using (db)
            {
                DStin = (from b in db.News select b).ToList();

            }
            return View(DStin);
        }

        ////
        //// GET: /News/Create

        public ActionResult Themtintuc()
        {
            return View();
        }

        //
        // POST: /News/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Themtintuc(News news)
        {
            if (ModelState.IsValid)
            {
                news.CreaDate = DateTime.Now;
                if (Session["HoTen"] != null)
                {
                    news.UserAdd = Session["HoTen"].ToString();
                }

                //var image = WebImage.GetImageFromRequest("file");
                //if (image != null)
                //{
                //    if (image.Width > 100)
                //    {
                //        image.Resize(100, 120);

                //    }
                //    //var filename = Path.GetFileName(image.FileName);

                //    //Lấy type của ảnh
                //    string type = Path.GetFileName(image.FileName);
                //    //Trường hợp ảnh là jpg chuyển thành jpeg
                //    var path = Path.Combine(Server.MapPath("~/Content/Update"), type);
                //    image.Save(path); //Lưu ảnh trên server


                //}
                //var fileName = Path.GetFileName(news.Images);
                //var path = Path.Combine(Server.MapPath("/Content/Upload"), fileName);
                //ViewBag.filename = path;



                //var fileName = Path.GetFileName(news.Images);
                //var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
                //news.Save(path);
                //ViewBag.Message = "File has been uploaded successfully";
                //ModelState.Clear();/////////////////////////
                db.News.Add(news);
                db.SaveChanges();
                //var extension = Path.GetExtension(news.Images.FileName);
                //var path = Path.Combine(Server.MapPath("~/Content/Upload"));
                //if (!Directory.Exists(path))
                //    Directory.CreateDirectory(path);
                //news.Images.SaveAs(path + news.ID + extension);
                
                return RedirectToAction("DSTin");
            }

            return View(news);
        }

        //
        // GET: /News/Edit/5

        public ActionResult Suatintuc(int id)
        {
            News news = db.News.Find(id);
            return View(news);
        }

        //
        // POST: /News/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Suatintuc(News news)
        {
            if (ModelState.IsValid)
            {
                news.CreaDate = DateTime.Now;
                if (Session["HoTen"] != null)
                {
                    news.UserAdd = Session["HoTen"].ToString();
                } 
                db.Entry(news).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("DSTin");
            }
            return View(news);
        }

        //
        // GET: /News/Delete/5

        //public ActionResult Xoatintuc(int id)
        //{
        //    News news = db.News.Find(id);
        //    return View(news);
        //}

        //
        // POST: /News/Delete/5

        public ActionResult Xoatintuc(int id)
        {
            News news = db.News.Find(id);
            db.News.Remove(news);
            db.SaveChanges();
            return RedirectToAction("DSTin");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}